//
//  AccountViewController.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/30.
//

import UIKit

class AccountViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubviews([
            logoImageV,
            bottomView
        ])
        view.addSubview(backButton)
        bottomView.addSubviews([
            nameView,
            logoutBtn,
            deleteBtn
        ])
        nameView.addSubviews([
            accountImageV,
            accountLabel,
            accountValueLabel
        ])
        backButton.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalToSuperview().offset(CGFloat.statusHeight)
            make.width.equalTo(56)
            make.height.equalTo(44)
        }
        logoImageV.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(365)
        }
        bottomView.snp.makeConstraints { make in
            make.bottom.leading.trailing.equalToSuperview()
            make.height.equalTo(365)
            make.top.equalTo(logoImageV.snp.bottom).offset(-45)
        }
        nameView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(52)
            make.top.equalToSuperview().offset(36)
        }
        deleteBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(325)
            make.height.equalTo(56)
            make.bottom.equalToSuperview().offset(-40)
        }
        logoutBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(325)
            make.height.equalTo(56)
            make.bottom.equalTo(deleteBtn.snp.top).offset(-15)
        }
        accountImageV.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.leading.equalToSuperview().offset(18)
            make.centerY.equalToSuperview()
        }
        accountLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(64)
            make.centerY.equalToSuperview()
            make.width.equalTo(70)
        }
        accountValueLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
            make.leading.equalTo(accountLabel.snp.trailing).offset(10)
        }
    }

    @objc private func clickLogoutButton(){
        
        UserDefaultsManager.clearUserDefaults()
        UIApplication.window?.rootViewController = BaseTabBarVC()
    }
    
    @objc private func clickDeleteButton(){
        
        Task{
            
            let res = await network.request(.delete)
            if res.isSuccess{
                showToast("Erfolgreich gelöscht")
                UserDefaultsManager.clearUserDefaults()
                UIApplication.window?.rootViewController = BaseTabBarVC()
            }
        }
    }
    
    @objc private func clickBackButton(){

        self.navigationController?.popViewController(animated: true)
    }
    
    private lazy var backButton: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(named: "back"), for: .normal)
        v.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        return v
    }()
    lazy var logoImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "mine_accountBg")
        v.isUserInteractionEnabled = true
        return v
    }()
    lazy var bottomView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 16
        v.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        v.layer.masksToBounds = true
        return v
    }()
    private lazy var nameView: UIView = {
        let v = UIView()
        v.layer.cornerRadius = 8
        v.layer.masksToBounds = true
        v.backgroundColor = UIColor(hexString: "#F9F9F9")
        return v
    }()
    private lazy var accountImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "mine_userName")
        return v
    }()
    private lazy var accountLabel: UILabel = {
        let v = UILabel()
        v.textColor = UIColor(hexString: "#4B5062")
        v.font = UIFont.systemFont(ofSize: 17)
        v.text = "Konto"
        return v
    }()
    private lazy var accountValueLabel: UILabel = {
        let v = UILabel()
        v.textColor = UIColor(hexString: "#A1A1A1")
        v.font = UIFont.systemFont(ofSize: 17)
        v.textAlignment = .right
        if let userName = UserDefaultsManager.getString(forKey: .userName) {
            v.text = userName
        }
        return v
    }()
    private lazy var logoutBtn: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(named: "logout"), for: .normal)
        v.addTarget(self, action: #selector(clickLogoutButton), for: .touchUpInside)
        return v
    }()
    private lazy var deleteBtn: UIButton = {
        let v = UIButton(type: .custom)
        v.setTitle("Konto löschen", for: .normal)
        v.setTitleColor(UIColor(hexString: "#4B5062"), for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        v.addTarget(self, action: #selector(clickDeleteButton), for: .touchUpInside)
        v.layer.cornerRadius = 28
        v.layer.borderColor = UIColor(hexString: "#4B5062").cgColor
        v.layer.borderWidth = 1
        v.layer.masksToBounds = true
        return v
    }()
}
